package com.example.lin.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.pojo.HotData;
import com.example.lin.ximalaya.pojo.Type20Or23ListItem;
import com.example.lin.ximalaya.utils.NumberUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Type20or13ViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rv_item)
    RecyclerView rvItem;
    @BindView(R.id.rv_keyword)
    RecyclerView rv_keyword;
    private KeyWorldRVApdater keyWorldRVApdater;
    private HotBookRVAdapter hotBookRVAdapter;


    @OnClick(R.id.ll_change)
    public void change(View view) {
        Toast.makeText(context, "点击了换一批!", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.tv_more)
    public void more(View view) {
        Toast.makeText(context, "点击了更多!", Toast.LENGTH_SHORT).show();
    }

    public Type20or13ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {


        rvItem.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));


        rv_keyword.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

        keyWorldRVApdater = new KeyWorldRVApdater(context);
        rv_keyword.setAdapter(keyWorldRVApdater);

        hotBookRVAdapter = new HotBookRVAdapter(context);
        rvItem.setAdapter(hotBookRVAdapter);
    }

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    public void setData(HotData.ListBeanX data) {
        tvTitle.setText(data.getTitle());

        //处理第一个RecyclerView
        List<HotData.ListBeanX.KeywordsBean> keywords = data.getKeywords();
        keyWorldRVApdater.setKeywords(keywords);

        //处理第二个RecyclerView
        List<JsonObject> list = data.getList();
        ArrayList<Type20Or23ListItem> listItems = new ArrayList<>();
        for (JsonObject obj : list) {
            Type20Or23ListItem listItem = new Gson().fromJson(obj, Type20Or23ListItem.class);
            listItems.add(listItem);
        }

        String direction = data.getDirection();
        if ("column".equals(direction)) {
            rvItem.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        } else {
            rvItem.setLayoutManager(new GridLayoutManager(context,3, LinearLayoutManager.VERTICAL, false));
        }
        hotBookRVAdapter.setListItems(listItems, direction);
    }


    class KeyWorldRVApdater extends RecyclerView.Adapter<KeyWorldRVApdater.KeyWorldViewHolder> {

        List<HotData.ListBeanX.KeywordsBean> keywords;
        Context context;

        public KeyWorldRVApdater(Context context) {
            this.context = context;
        }

        public void setKeywords(List<HotData.ListBeanX.KeywordsBean> keywords) {
            this.keywords = keywords;
            notifyDataSetChanged();
        }


        @NonNull
        @Override
        public KeyWorldViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new KeyWorldViewHolder(LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_20_rv1_item, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull KeyWorldViewHolder holder, int position) {
            holder.tvKeyworld.setText(keywords.get(position).getKeywordName());
        }

        @Override
        public int getItemCount() {
            return keywords == null ? 0 : 4;
        }

        class KeyWorldViewHolder extends RecyclerView.ViewHolder {

            TextView tvKeyworld;

            public KeyWorldViewHolder(View itemView) {
                super(itemView);
                tvKeyworld = (TextView) itemView;
            }
        }
    }


    class HotBookRVAdapter extends RecyclerView.Adapter {

        private String direction;
        ArrayList<Type20Or23ListItem> listItems;
        Context context;

        public HotBookRVAdapter(Context context) {
            this.context = context;
        }

        public void setListItems(ArrayList<Type20Or23ListItem> listItems, String direction) {
            this.listItems = listItems;
            this.direction = direction;
            notifyDataSetChanged();
        }

        int TYPE_ROW = 123;
        int TYPE_COLUMN = 321;


        @Override
        public int getItemViewType(int position) {
            return "column".equals(direction) ? TYPE_COLUMN : TYPE_ROW;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if (viewType == TYPE_COLUMN) {
                View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_20_rv2_item, parent, false);
                return new HotBookColumnViewHolder(layout);
            } else {
                View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_23_rv_item, parent, false);
                return new HotBookRowViewHolder(layout);
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            Type20Or23ListItem item = listItems.get(position);
            if (holder instanceof HotBookColumnViewHolder) {
                HotBookColumnViewHolder hotBookColumnViewHolder = (HotBookColumnViewHolder) holder;
                Glide.with(context).load(item.getPic()).into(hotBookColumnViewHolder.ivIcon);
                hotBookColumnViewHolder.tvTitle.setText(item.getTitle());
                hotBookColumnViewHolder.tvSubtitle.setText(item.getSubtitle());
                hotBookColumnViewHolder.tvTrack.setText(String.valueOf(item.getTracksCount()));
                int playsCount = item.getPlaysCount();
                String s = NumberUtils.number2String(playsCount);
                hotBookColumnViewHolder.tvNum.setText(s);
            } else if (holder instanceof HotBookRowViewHolder) {
                HotBookRowViewHolder hotBookRowViewHolder = (HotBookRowViewHolder) holder;


                Type20Or23ListItem type23listItem = listItems.get(position);
                hotBookRowViewHolder.tvTitle.setText(type23listItem.getTitle());
                hotBookRowViewHolder.tv_subtitle.setText(type23listItem.getSubtitle());
                Glide.with(context).load(type23listItem.getPic()).into(hotBookRowViewHolder.ivIcon);
                int playsCount = type23listItem.getPlaysCount();
                String s = NumberUtils.number2String(playsCount);
                hotBookRowViewHolder.tvNum.setText(s);
            }
        }

        @Override
        public int getItemCount() {
            return listItems == null ? 0 : listItems.size();
        }

        class HotBookRowViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_num_icon)
            ImageView iv_num_icon;
            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.rv_icon)
            CardView rvIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_jp)
            TextView tv_jp;
            @BindView(R.id.tv_vip)
            TextView tv_vip;
            @BindView(R.id.tv_subtitle)
            TextView tv_subtitle;


            HotBookRowViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);

                //确定 CardView 的宽高
                int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
                float spacing = context.getResources().getDimension(R.dimen.dimens_5dp);
                int cardViewWidth = (int) ((widthPixels - 8 * spacing) / 3);

                ViewGroup.LayoutParams lp = rvIcon.getLayoutParams();
                lp.width = cardViewWidth;
                lp.height = cardViewWidth;
                rvIcon.setLayoutParams(lp);
                iv_num_icon.setImageResource(R.mipmap.play);
            }
        }

        class HotBookColumnViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.cv_icon)
            CardView cvIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;
            @BindView(R.id.tv_subtitle)
            TextView tvSubtitle;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.tv_track)
            TextView tvTrack;
            @BindView(R.id.ll_bottom)
            LinearLayout llBottom;

            HotBookColumnViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
}
